package com.ly.system.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.utils.SqlUtils;
import com.ly.system.mapper.SysLogMapper;
import com.ly.system.service.ISysLogService;
import com.ly.system.system.bean.entity.SysLog;
import com.ly.system.system.bean.vo.IndexVisitInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 系统日志-服务
 *
 * @author jeecg
 * @since 2018-12-26
 */
@Service
@RequiredArgsConstructor
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    private final SysLogMapper sysLogMapper;

    @Override
    public R getPage(SysLog entity, Page<SysLog> page) {
        IPage<SysLog> pageList = this.page(
                page,
                new LambdaQueryWrapper<SysLog>()
                        .eq(ObjUtil.isNotEmpty(entity.getLogType()), SysLog::getLogType, entity.getLogType())
                        .like(StrUtil.isNotBlank(entity.getLogContent()), SysLog::getLogContent, entity.getLogContent())
                        .ge(ObjUtil.isNotEmpty(entity.getStartTime()), SysLog::getCreateTime, entity.getStartTime())
                        .le(ObjUtil.isNotEmpty(entity.getEndTime()), SysLog::getCreateTime, entity.getEndTime())
                        .orderByDesc(SysLog::getCreateTime)
        );
        return R.ok(pageList);
    }

    @Override
    public Long findTotalVisitCount() {
        return sysLogMapper.findTotalVisitCount();
    }

    @Override
    public Long findTodayVisitCount(Date dayStart, Date dayEnd) {
        return sysLogMapper.findTodayVisitCount(dayStart, dayEnd);
    }

    @Override
    public Long findTodayIp(Date dayStart, Date dayEnd) {
        return sysLogMapper.findTodayIp(dayStart, dayEnd);
    }

    @Override
    public List<IndexVisitInfoVO> findVisitCount(Date dayStart, Date dayEnd) {
        DbType dbType = SqlUtils.getDatabaseTypeEnum();
        return sysLogMapper.findVisitCount(dayStart, dayEnd, dbType.getDb());
    }
}
